package chapter26;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/28 17:23
 */
public class GroupTest {
    public static void main(String[] args) {
        List<Student> students = Arrays.asList(new Student[]{
                new Student("zhangsan", 87.2, "1"),
                new Student("lisi", 89.2, "2"),
                new Student("li", 87.2, "1"),
                new Student("sunqi", 77.2, "2"),
                new Student("wangwu", 97.4, "1")});

        Map<String, List<Student>> groups = students.stream().collect(Collectors.groupingBy(Student::getGrade));
        System.out.println(groups);

        Map<String, Long> gradeCountMap = students.stream().collect(Collectors.groupingBy(Student::getGrade, Collectors.counting()));
        System.out.println(gradeCountMap);

        Map<String, Long> wordCountMap = Stream.of("hello", "world", "abc", "hello", "abc").collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
        System.out.println(wordCountMap);

        Map<Boolean, List<Student>> byPass = students.stream().collect(Collectors.partitioningBy(t -> t.getScore() > 90));
        System.out.println(byPass);
    }
}
